#ifndef __PERSON_HPP__
#define __PERSON_HPP__

#include <iostream>
#include <algorithm>
#include <string>
#include <list>

#include <cstring>
#include <cstdlib>

#include "funcs.hpp"

using namespace std;

struct Person {
  string name;
  list<string> akas;
  list<string> actor;
  list<string> director;
  list<string> producer;
  list<string> writer;
  
  Person(const string& str) : name(str) {}

  void add_aka(const string& str, bool check_reps = true)
  {
    if ( !check_reps || ( check_reps && find(akas.begin(), akas.end(), str) == akas.end() ) )
      akas.push_back(str);
  }

  void add_actor(const string& str, bool check_reps = true)
  {
    if ( !check_reps || ( check_reps && find(actor.begin(), actor.end(), str) == actor.end() ) )
    actor.push_back(str);
  }

  void add_director(const string& str, bool check_reps = true)
  {
    if ( !check_reps || ( check_reps && find(director.begin(), director.end(), str) == director.end() ) )
    director.push_back(str);
  }

  void add_producer(const string& str, bool check_reps = true)
  {
    if ( !check_reps || ( check_reps && find(producer.begin(), producer.end(), str) == producer.end() ) )
    producer.push_back(str);
  }

  void add_writer(const string& str, bool check_reps = true)
  {
    if ( !check_reps || ( check_reps && find(writer.begin(), writer.end(), str) == writer.end() ) )
      writer.push_back(str);
  }

  friend ostream& operator << (ostream& os, const Person& p)
  {
    if ( p.actor.empty() && p.director.empty() && p.producer.empty() && p.writer.empty() )
      return os;

    os << "<person name=\"" << xml_escape(p.name) << "\">" << endl;
    
    if ( !p.akas.empty() ) {
      os << "  <akas>" << endl;
      for(list<string>::const_iterator it = p.akas.begin();
	  it != p.akas.end(); ++it) 
	{
	  os << "    <aka>" << xml_escape(*it) << "</aka>" << endl;
	}
      os << "  </akas>" << endl;
    }
    
    if ( !p.actor.empty() ) {
      os << "  <actor>" << endl;
      for(list<string>::const_iterator it = p.actor.begin();
	  it != p.actor.end(); ++it) 
	{
	  os << "    <movie>" << xml_escape(*it) << "</movie>" << endl;
	}
      os << "  </actor>" << endl;
    }
    
    if ( !p.director.empty() ) {
      os << "  <director>" << endl;
      for(list<string>::const_iterator it = p.director.begin();
	  it != p.director.end(); ++it) 
	{
	  os << "    <movie>" << xml_escape(*it) << "</movie>" << endl;
	}
      os << "  </director>" << endl;
    }

    if ( !p.producer.empty() ) {
      os << "  <producer>" << endl;
      for(list<string>::const_iterator it = p.producer.begin();
	  it != p.producer.end(); ++it) 
	{
	  os << "    <movie>" << xml_escape(*it) << "</movie>" << endl;
	}
      os << "  </producer>" << endl;
    }

    if ( !p.writer.empty() ) {
      os << "  <writer>" << endl;
      for(list<string>::const_iterator it = p.writer.begin();
	  it != p.writer.end(); ++it) 
	{
	  os << "    <movie>" << xml_escape(*it) << "</movie>" << endl;
	}
      os << "  </writer>" << endl;
    }

    os << "</person>" << endl;

    return os;
  }

};

#endif
